package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.EnglishWord;
import com.xjh.t.domain.Sentence;
import com.xjh.t.domain.User;
import com.xjh.t.service.IEnglishWordService;
import com.xjh.t.service.ISentenceService;
import com.xjh.t.vo.EnglishWordVo;
import com.xjh.t.vo.ExpressInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/englishWord")
public class EnglishWordController {

    @Autowired
    private IEnglishWordService englishWordService;

    @Autowired
    private ISentenceService sentenceService;

    @RequestMapping("/addWord")
    public ResultObj addWord(EnglishWord word) {

        QueryWrapper<EnglishWord> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("english_word",word.getEnglishWord());

        List<EnglishWord> list = this.englishWordService.list(queryWrapper);

        if(list.size() > 0)
        {
            return ResultObj.ADD_ERROR;
        }

        this.englishWordService.save(word);

        return ResultObj.ADD_SUCESS;

    }


    @RequestMapping("/loadAllWord")
    public DataGridView loadAllWord(EnglishWordVo word) {

        IPage<EnglishWord> page=new Page<>(word.getPage(), word.getLimit());
        QueryWrapper<EnglishWord> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(word.getEnglishWord()), "english_word",word.getEnglishWord());
        queryWrapper.like(StringUtils.isNotBlank(word.getChineseWord()), "chinese_word",word.getChineseWord());


        this.englishWordService.page(page, queryWrapper);

        List<EnglishWordVo> englishWordVos = new ArrayList<>();

        for(EnglishWord word1:page.getRecords())
        {
            EnglishWordVo englishWordVo = new EnglishWordVo();
            BeanUtils.copyProperties(word1,englishWordVo);
            List<Sentence> sentenceList = new ArrayList<>();
            if(StringUtils.isNotBlank(word1.getSentenceIds()))
            {
                String[] sentenceids = StringUtils.split(word1.getSentenceIds(),",");
                for(String senid:sentenceids)
                {
                    if(sentenceService.getById(Integer.parseInt(senid)) != null)
                    {
                        sentenceList.add(sentenceService.getById(Integer.parseInt(senid)));
                    }
                }
            }
            englishWordVo.setSentenceList(sentenceList);

            englishWordVos.add(englishWordVo);
        }

        return new DataGridView(page.getTotal(),englishWordVos);

    }

    @RequestMapping("/updateWord")
    public ResultObj updateWord(EnglishWord word) {

        this.englishWordService.updateById(word);

        return ResultObj.UPDATE_SUCESS;

    }
}

